/**
 * 自定义指令的封装
 * @type {{throttle: {unmounted(*, *): void, beforeMount(*, *): void}, debounce: {unmounted(*, *): void, beforeMount(*, *): void}, longpress: {unmounted(*): void, beforeMount(*, *): (*|void|undefined), updated(*, *): void}, copy: {unmounted(*): void, beforeMount(*, *): void, update(*, *): void}, realImg: {beforeMount(*, *): Promise<void>}}}
 * @private
 */
export const _directiveObjArr = {
  /**
   * 长按事件的触发指令
   */
  longpress: {
    beforeMount(el, binding) {
      const cb = binding.value
      el.$duration = binding.arg || 3000
      if (typeof cb !== 'function') { return console.log('v-longpress指令必须接受一个回调函数！') }
      let timer = null
      const add = (e) => {
        if (e.type === 'click' && e.button !== 0) return
        e.preventDefault()
        if (timer === null) {
          timer = setTimeout(() => {
            cb()
            timer = null
          }, el.$duration)
        }
      }
      const cancel = () => {
        if (timer !== null) {
          clearTimeout(timer)
          timer = null
        }
      }
      el.addEventListener('mousedown', add)
      el.addEventListener('touchstart', add)
      el.addEventListener('click', cancel)
      el.addEventListener('mounseout', cancel)
      el.addEventListener('touchend', cancel)
      el.addEventListener('touchcancel', cancel)
    },
    updated(el, binding) {
      el.$duration = binding.arg
    },
    unmounted(el) {
      el.removeEventListener('mousedown', () => { })
      el.removeEventListener('touchstart', () => { })
      el.removeEventListener('click', () => { })
      el.removeEventListener('mounseout', () => { })
      el.removeEventListener('touchend', () => { })
      el.removeEventListener('touchcancel', () => { })
    }
  },

  /**
   * 节流
   * @private
   */
  throttle: {
    beforeMount(el, binding) {
      let _stop = false
      el.addEventListener(binding.arg, (e) => {
        if (_stop) return
        binding.value(e)
        _stop = true
        setTimeout(() => {
          _stop = false
        }, 1000)
      })
    },
    unmounted(el, binding) {
      el.removeEventListener(binding.arg, () => { })
    }
  },

  /**
   * 防抖
   * @private
   */
  debounce: {
    beforeMount(el, binding) {
      let stopTime = null
      const handler = () => {
        clearTimeout(stopTime)
        stopTime = setTimeout(binding.value, 200)
      }
      el._debounceHandler = handler
      el.addEventListener(binding.arg, handler)
    },
    beforeUnmount(el, binding) {
      el.removeEventListener(binding.arg, el._debounceHandler)
    }
  },

  /**
   * 一键复制
   * @method debounce
   */
  copy: {
    beforeMount(el, binding) {
      el.targetContent = binding.value
      const success = binding.arg
      el.addEventListener('click', () => {
        if (!el.targetContent) return console.log('没有需要复制的文本内容！')
        const textArea = document.createElement('textarea')
        textArea.readOnly = 'readonly'
        textArea.style.position = 'fixed'
        textArea.style.top = '-99999px'
        textArea.value = el.targetContent
        document.body.appendChild(textArea)
        textArea.select()
        const res = document.execCommand('copy')
        res && success
          ? success(el.targetContent)
          : console.log('复制成功，剪贴板内容为: ', el.targetContent)
        document.body.removeChild(textArea)
      })
    },
    update(el, binding) {
      el.targetContent = binding.value
    },
    unmounted(el) {
      el.removeaddEventListener('click', () => { })
    }
  },
  /**
   * 在图片请求失败之后用另一张图片做代替
   * @param
   */
  realImg: {
    async beforeMount(el, binding) {
      const _imgURL = binding.value
      if (_imgURL) {
        const exist = await imageIsExist(_imgURL)
        exist && el.setAttribute('src', _imgURL)
      }
      function imageIsExist(url) {
        return new Promise((resolve) => {
          let img = new Image()
          img.src = url
          img.onload = () => {
            if (img?.complete) {
              resolve(true)
              img = null
            }
          }
          img.onerror = () => {
            resolve(false)
            img = null
          }
        })
      }
    }
  }
}
